package com.yantong.service.impl;

import com.yantong.constant.CategoryType;
import com.yantong.mapper.CategoryMapper;
import com.yantong.mapper.ProductGradeMapper;
import com.yantong.pojo.Category;
import com.yantong.pojo.CategoryExample;
import com.yantong.pojo.ProductGrade;
import com.yantong.service.CategoryService;
import com.yantong.service.ProductCentreService;
import com.yantong.utils.PageUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
	private static final Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

	@Autowired
	private CategoryMapper categoryMapper;
	@Autowired
	private ProductGradeMapper productGradeMapper;

	@Override
	public List<Category> listCategorys() {
		List<Category> categoryList = new ArrayList<>();

		CategoryExample example = new CategoryExample();
		CategoryExample.Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(1);
		//分类排序
		example.setOrderByClause("displayorder asc");
		categoryList = categoryMapper.selectByExample(example);
//		Long total = categoryMapper.countByExample(example);
//		if (total >0) {
//			categoryList = categoryMapper.selectByExample(example);
//			for (Category vo : categoryList) {
//				CategoryExample example1 = new CategoryExample();
//				CategoryExample.Criteria criteria1 = example1.createCriteria();
//				criteria1.andParentidEqualTo(vo.getId());
//				vo.setCategoryList(categoryMapper.selectByExample(example1));
//			}
//        }
		return categoryList;
	}

	@Override
	public List<Category> listCategorys(Category category) {
		List<Category> categoryList = new ArrayList<>();

		CategoryExample example = new CategoryExample();
		CategoryExample.Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(1);
		if (category.getCentreId() != null) {
			criteria.andCentreIdEqualTo(category.getCentreId());
		}
		if (category.getLiansuoId() != null) {
			criteria.andLiansuoIdEqualTo(category.getLiansuoId());
		}
		//分类排序
		example.setOrderByClause("displayorder asc");
		categoryList = categoryMapper.selectByExample(example);
//		Long total = categoryMapper.countByExample(example);
//		if (total >0) {
//			categoryList = categoryMapper.selectByExample(example);
//			for (Category vo : categoryList) {
//				CategoryExample example1 = new CategoryExample();
//				CategoryExample.Criteria criteria1 = example1.createCriteria();
//				criteria1.andParentidEqualTo(vo.getId());
//				vo.setCategoryList(categoryMapper.selectByExample(example1));
//			}
//        }
		return categoryList;
	}

	@Override
	public int save(Category obj) {
		if (obj == null) {
			return 0;
		}
		if(obj.getParentid() != null && obj.getParentid() != 0){
			Category category = categoryMapper.selectByPrimaryKey(obj.getParentid());
			obj.setLiansuoId(category.getLiansuoId());
			obj.setProductGradeId(category.getProductGradeId());
		}

		return categoryMapper.insertSelective(obj);
	}

	@Override
	public Category getByKey(Long id) {
		if (id == null) {
			return null;
		}
		return categoryMapper.selectByPrimaryKey(id);
	}

	@Override
	public int update(Category obj) {
		if (obj == null || obj.getId() == null) {
			return 0;
		}
		if (obj.getEnabled() == null || obj.getEnabled() != 1) {
			obj.setEnabled(0);
		}
		return categoryMapper.updateByPrimaryKeySelective(obj);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int remove(Long id) {
		if (id == null) {
			return 0;
		}
		Category obj = new Category();
		obj.setId(id);
		obj.setStatus(0);

		categoryMapper.delByParentId(id);
		return categoryMapper.updateByPrimaryKeySelective(obj);
	}

	@Override
	public List<Category> listCategorys(Long parentId) {
		CategoryExample example = new CategoryExample();
		CategoryExample.Criteria criteria = example.createCriteria();
		criteria.andParentidEqualTo(parentId);
		criteria.andStatusEqualTo(1);
		//分类排序
		example.setOrderByClause("displayorder asc");
		return categoryMapper.selectByExample(example);
	}

	@Override
	public List<Category> listCategorys(Long parentId, Integer type, Long centreId,Long liansuoId) {
		CategoryExample example = new CategoryExample();
		CategoryExample.Criteria criteria = example.createCriteria();
		criteria.andParentidEqualTo(parentId)
				.andStatusEqualTo(1);
		if (type != null) {
			criteria.andTypeEqualTo(type);
		}
		if (centreId != null) {
			criteria.andCentreIdEqualTo(centreId);
		}
		if(liansuoId != null){
			criteria.andLiansuoIdEqualTo(liansuoId);
		}
		//分类排序
		example.setOrderByClause("displayorder asc");
		return categoryMapper.selectByExample(example);
	}

	@Override
	public Long selectByName(String name) {
		Long id = categoryMapper.selectByName(name);
		return id == null ? 0 : id;
	}

	@Override
	public Long selectByNameSon(Map map) {
		return categoryMapper.selectByNameSon(map);
	}


	@Override
	public Category selectCategoryByName(Map map) {
		return categoryMapper.selectCategoryByName(map);
	}

	@Override
	public List<Category> getListCategory(Category category) {
		List<Category> list = categoryMapper.getListCategory(category);
		//查得是一级分类
		if (category.getParentid() != null && category.getParentid() == 0) {
			for (Category c : list) {
				//是一级分类
				if (c.getParentid() != null && c.getParentid() == 0) {
					//添加所有二级分类
					c.setCategoryList(this.listCategorys(c.getId()));
				}
			}
		}
		return list;
	}




	@Override
	public int copy(Long id) {
		Category category = categoryMapper.selectByPrimaryKey(id);
		category.setId(null);
		return categoryMapper.insertSelective(category);
	}

	@Override
	public PageUtils<Category> queryCategoryPage(Category category, int offset, int limit) {
		PageUtils<Category> pageUtils = new PageUtils<>();
		int total = categoryMapper.getListCategoryCount(category);
		if (total > 0) {
			logger.info("listCommanders, total size:{}", total);
			pageUtils.setTotal(total);
			List<Category> categoryList = categoryMapper.getListCategory(category, new RowBounds(offset, limit));
			Map<Long, ProductGrade> productGradeMap = new HashMap<>();
			List<ProductGrade> productGradeList = productGradeMapper.selectByAll();
			for (ProductGrade productGrade : productGradeList){
				productGradeMap.put(productGrade.getId(),productGrade);
			}

			for (Category categoryResult : categoryList) {
				if (categoryResult.getType() == CategoryType.houtaileimu.getType()) {
					categoryResult.setTypeName("活动类目");
				} else if (categoryResult.getType() == CategoryType.qiantaileimu.getType()) {
					categoryResult.setTypeName("前台类目");
				} else if (categoryResult.getType() == CategoryType.houtaileimu.getType()) {
					categoryResult.setTypeName("后台类目");
				} else if (categoryResult.getType() == CategoryType.zhuyeleimu.getType()) {
					categoryResult.setTypeName("主页类目");
				} else if (categoryResult.getType() == CategoryType.waimaileimu.getType()) {
					categoryResult.setTypeName("外卖类目");
				}

				if( categoryResult.getProductGradeId() != null ){
					ProductGrade productGrade = productGradeMap.get(categoryResult.getProductGradeId().longValue());
					if( productGrade != null ){
						categoryResult.setProductGradeIdName(productGrade.getName());
					}
				}
			}
			pageUtils.setRows(categoryList);
		} else {
			logger.info("listCommanders not found");
			pageUtils.setRows(new ArrayList<>());
		}
		return pageUtils;
	}

	@Override
	public List<Category> selectCategoryByProductGradeId(Long ProductGradeId) {
		return categoryMapper.selectCategoryByProductGradeId(ProductGradeId);
	}

	@Override
	public List<Category> selectByParentid(Long parentId) {
		return categoryMapper.selectByParentid(parentId);
	}
}
